package net.oschina.gitapp.media;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;


class CropDrawable extends Drawable {
    private Context mContext;
    private Paint mCornerPaint = new Paint();
    private Paint mLinePaint = new Paint();
    private Paint mNineLinePaint = new Paint();

    private int mCropWidth = 800, mCropHeight = 800;

    private int mLeft, mRight, mTop, mBottom;

     CropDrawable(Context mContext) {
        this.mContext = mContext;
        initPaint();
    }

    private void initPaint() {
        mLinePaint.setColor(Color.WHITE);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(2);
        mLinePaint.setStyle(Paint.Style.STROKE);

        mNineLinePaint.setColor(Color.WHITE);
        mNineLinePaint.setAntiAlias(true);
        mNineLinePaint.setStrokeWidth(1);
        mNineLinePaint.setStyle(Paint.Style.STROKE);

        mCornerPaint.setColor(Color.WHITE);
        mCornerPaint.setAntiAlias(true);
        mCornerPaint.setStrokeWidth(8);
        mCornerPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        int width = Util.getScreenWidth(mContext);
        int height = Util.getScreenHeight(mContext);
        mLeft = (width - mCropWidth) / 2;
        mTop = (height - mCropHeight) / 2;
        mRight = (width + mCropWidth) / 2;
        mBottom = (height + mCropHeight) / 2;
        Rect rect = new Rect(mLeft, mTop, mRight, mBottom);
        canvas.drawRect(rect, mLinePaint);
        //左上
        canvas.drawLine(mLeft, mTop, mLeft, mTop + 50, mCornerPaint);
        canvas.drawLine(mLeft - 4, mTop, mLeft + 50, mTop, mCornerPaint);

        //右上
        canvas.drawLine(mRight, mTop, mRight, mTop + 50, mCornerPaint);
        canvas.drawLine(mRight - 50, mTop, mRight + 4, mTop, mCornerPaint);

        //左下
        canvas.drawLine(mLeft, mBottom, mLeft + 50, mBottom, mCornerPaint);
        canvas.drawLine(mLeft, mBottom - 50, mLeft, mBottom + 4, mCornerPaint);

        //右下
        canvas.drawLine(mRight, mBottom, mRight, mBottom - 50, mCornerPaint);
        canvas.drawLine(mRight - 50, mBottom, mRight + 4, mBottom, mCornerPaint);

        int index = canvas.save();
        canvas.clipRect(rect);
        //画九宫格
        int vAvg = mCropWidth / 3;
        int hAvg = mCropHeight / 3;
        canvas.drawLine(mLeft + vAvg, mTop, mLeft + vAvg, mBottom, mNineLinePaint);
        canvas.drawLine(mLeft + vAvg * 2, mTop, mLeft + vAvg * 2, mBottom, mNineLinePaint);

        canvas.drawLine(mLeft, mTop + hAvg, mRight, mTop + hAvg, mNineLinePaint);
        canvas.drawLine(mLeft, mTop + hAvg * 2, mRight, mTop + hAvg * 2, mNineLinePaint);

        canvas.restoreToCount(index);
    }

    @Override
    public void setAlpha(int alpha) {

    }


    @Override
    public void setBounds(@NonNull Rect bounds) {
        super.setBounds(new Rect(mLeft, mTop, mRight, mBottom));
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }

     void setRegion(Rect rect) {
        int width = Util.getScreenWidth(mContext);
        int height = Util.getScreenHeight(mContext);
        rect.set((width - mCropWidth) / 2, (height - mCropHeight) / 2, (width + mCropWidth) / 2, (height + mCropHeight) / 2);
    }




    public int getTop() {
        return mCropHeight;
    }


    public int getBottom() {
        return mBottom;
    }

     void setCropWidth(int mCropWidth) {
        this.mCropWidth = mCropWidth;
    }

     void setCropHeight(int mCropHeight) {
        this.mCropHeight = mCropHeight;
    }
}
